package xhwl.hmo.mall.ui.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.ui.MessageActivity;
import xhwl.hmo.mall.ui.SearchActivity;
import xhwl.hmo.mall.views.HintPopupWindow;

/**
 * Created by admin on 2017-10-20.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {
    private IndicatorViewPager indicatorViewPager;
    ImageView hmo_directory;
    private HintPopupWindow hintPopupWindow;
    LinearLayout lin_search;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        return view;
    }

    @Override
    protected void initView(View view) {
        Resources res = getResources();
        lin_search = (LinearLayout) view.findViewById(R.id.lin_search);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.fragment_tabmain_viewPager);
        Indicator indicator = (Indicator) view.findViewById(R.id.fragment_tabmain_indicator);
        hmo_directory = (ImageView) view.findViewById(R.id.hmo_directory);
        indicator.setScrollBar(new ColorBar(getActivity(), Color.parseColor("#32a7df"), 10));
        float unSelectSize = 16;
        float selectSize = unSelectSize * 1f;

        int selectColor = res.getColor(R.color.light_blue);
        int unSelectColor = res.getColor(R.color.main_text_color);
        indicator.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor).setSize(selectSize, unSelectSize));

        viewPager.setOffscreenPageLimit(4);

        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);

        // 注意这里 的FragmentManager 是 getChildFragmentManager(); 因为是在Fragment里面
        // 而在activity里面用FragmentManager 是 getSupportFragmentManager()
        indicatorViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        Log.d("cccc", "Fragment 将要创建View " + this);
        ArrayList<String> strList = new ArrayList<>();
//        strList.add("扫一扫");
        strList.add("消息通知");
        //popupwindow初始化co
        ArrayList<View.OnClickListener> clickList = new ArrayList<>();
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getActivity(), "点击事件触发", Toast.LENGTH_SHORT).show();
                hintPopupWindow.dismissPopupWindow();
                startActivity(new Intent(getActivity(), MessageActivity.class));
            }
        };
        clickList.add(clickListener);
        clickList.add(clickListener);
        clickList.add(clickListener);
        clickList.add(clickListener);

        //具体初始化逻辑看下面的图
        hintPopupWindow = new HintPopupWindow(getActivity(), strList, clickList);
    }

    @Override
    protected void initListener() {

        hmo_directory.setOnClickListener(this);
        lin_search.setOnClickListener(this);
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hmo_directory:
                hintPopupWindow.showPopupWindow(v);
                break;
            case R.id.lin_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    private class MyAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_top, container, false);
            }
            TextView textView = (TextView) convertView;
            switch (position) {
                case 0:
                    textView.setText("推荐");
                    break;
                case 1:
                    textView.setText("基础卡");
                    break;
                case 2:
                    textView.setText("重疾卡");
                    break;
                case 3:
                    textView.setText("健康套装");
                    break;
            }

            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            Fragment mainFragment = null;
            if (position == 0) {
                mainFragment = new RecommendFragment();
                Bundle bundle = new Bundle();
//			bundle.putString(SecondLayerFragment.INTENT_STRING_TABNAME, tabName);
//			bundle.putInt(SecondLayerFragment.INTENT_INT_POSITION, position);
                mainFragment.setArguments(bundle);
            } else {
                mainFragment = new BasicsFragment();
                Bundle bundle = new Bundle();
			    bundle.putString("cardStatus",""+position);
//			bundle.putInt(SecondLayerFragment.INTENT_INT_POSITION, position);
                mainFragment.setArguments(bundle);
            }

            return mainFragment;
        }
    }
}
